package shop.controls;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import shop.pojo.Goods;
import shop.services.GoodsServices;

/**
 * Servlet implementation class GoodsAdd
 */
public class GoodsAdd extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GoodsAdd() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		FileItemFactory factory = new DiskFileItemFactory();
		ServletFileUpload fileup =new ServletFileUpload(factory);
		try {
			//FileItem是request对象中单个的字段的封装对象
			List<FileItem> list=fileup.parseRequest(request);//解析request
			//拆完request后遍历哪个是文件对象（如果是文件对象则写入到服务器端）
			Goods goods = new Goods();
			for (FileItem f : list) {
				if(f.isFormField()){
					if(f.getFieldName().equals("g_name")){
						goods.setG_name(f.getString("utf-8"));
					}else if(f.getFieldName().equals("price")){
						goods.setPrice(Float.parseFloat(f.getString()));
					}else if(f.getFieldName().equals("numbers")){
						goods.setNumbers(Integer.parseInt(f.getString()));
					}else if(f.getFieldName().equals("gc_id")){
						goods.setGc_id(Integer.parseInt(f.getString()));
					}else if(f.getFieldName().equals("gb_id")){
						goods.setGb_id(Integer.parseInt(f.getString()));
					}else if(f.getFieldName().equals("is_on")){
						goods.setIs_on(Integer.parseInt(f.getString()));
					}else if(f.getFieldName().equals("is_on_push")){
						goods.setIs_on_push(Integer.parseInt(f.getString()));
					}else if(f.getFieldName().equals("g_desc")){
						goods.setG_desc(f.getString("utf-8"));
					}
	
				}else{
					//System.out.println(request.getRealPath("upload")+File.separator+f.getName());
					if(f.getName() != null && !f.getName().equals("")){
						goods.setG_pic(f.getName());
						/*System.out.println(request.getContextPath());
						System.out.println(request.getRealPath("/"));
						System.out.println(this.getServletContext().getRealPath("/"));
						System.out.println(request.getRequestURI());*/
						String cun = request.getRealPath("upload")+File.separator+f.getName();
						//System.out.println(cun);
						File file = new File(cun);//创建文件
						try {
							f.write(file);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			}
			//System.out.println(goods);
			int i =new GoodsServices().AddGoods(goods);
			if(i>0){
				request.getRequestDispatcher("GoodsSel.do?currentPage=0").forward(request, response);
			}else{
				response.sendRedirect("jsp2/goodsAdd.jsp");
			}
		} catch (FileUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
